﻿using System.Collections.Generic;
using Game;
namespace Mekiasm.ElectricElement
{
    public class ElectricDigtalElement : Common.ElectricElement
    {
        public int NowX = 0;
        public int NowY = 0;
        public int NowZ = 0;
        public bool FoundFlag = false;
        public TerrainChunk terrainChunk;
        public List<int> Minerals = new List<int>() {
            OreUranium.Index,DiamondOreBlock.Index,
            IronOreBlock.Index,CoalOreBlock.Index,
            CopperOreBlock.Index,OreTin.Index,
            OreLead.Index,
            GermaniumOreBlock.Index,
            SaltpeterOreBlock.Index,SulphurOreBlock.Index
        };

        public ComponentElectricDigtal ElectricDigtal;

        public ElectricDigtalElement(int x, int y, int z) : base(x, y, z)
        {
            ElectricDigtal = (ComponentElectricDigtal)Instance.Machine;
            terrainChunk = ILibrary.SubsystemTerrain.Terrain.GetChunkAtCell(Point.X, Point.Z);
            NowX = terrainChunk.Origin.X;
            NowZ = terrainChunk.Origin.Y;
        }
        public void DigMineral()
        {
            var Component = Instance.Machine;
            if (Component.Power >= Component.OutputPowerLimit && !FoundFlag)
            {
                for (int z = 0; z < 256; z++)
                {
                    if (NowY < 256)
                    {
                        int v = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(NowX, NowY, NowZ);
                        v = ILibrary.GetBasicValue(v);
                        if ((ElectricDigtal.WhiteList.Count == 0 && Minerals.Contains(v)) || (ElectricDigtal.WhiteList.Count > 0 && ElectricDigtal.WhiteList.Contains(v)))
                        {
                            FoundFlag = true;
                            List<BlockDropValue> blockDropValues = new List<BlockDropValue>();
                            if (ElectricDigtal.IsPrecise)
                            {
                                Component.Power -= Component.OutputPowerLimit;
                                blockDropValues.Add(new BlockDropValue() { Count = 1, Value = v });
                            }
                            else
                            {
                                BlocksManager.Blocks[Terrain.ExtractContents(v)].GetDropValues(null, v, 0, 4, blockDropValues, out bool sxzs);
                                Component.Power -= Component.OutputPowerLimit * 6;
                            }
                            bool flag = false;
                            foreach (BlockDropValue blockDrop in blockDropValues)
                            {
                                for (int n = 0; n < Component.SlotsCount; n++)
                                {
                                    if (Component.GetSlotCount(n) == 0 || Component.GetSlotValue(n) == ILibrary.GetBasicValue(blockDrop.Value) && Component.GetSlotCount(n) + blockDrop.Count <= Component.GetSlotCapacity(n, ILibrary.GetBasicValue(blockDrop.Value)))
                                    {
                                        flag = true;
                                        Component.AddSlotItems(n, blockDrop.Value, blockDrop.Count);
                                        break;
                                    }
                                }
                                if (flag) break;
                            }
                            if (flag) ILibrary.SubsystemTerrain.ChangeCell(NowX, NowY, NowZ, 0);
                        }
                        NowY += 1;
                    }
                    else
                    {
                        NowY = 0;
                        NowX += 1;
                        if (NowX >= terrainChunk.Origin.X + 16)
                        {
                            NowX = terrainChunk.Origin.X;
                            NowZ += 1;
                            if (NowZ >= terrainChunk.Origin.Y + 16)
                            {
                                NowZ = terrainChunk.Origin.Y;
                            }
                        }
                    }
                    if (FoundFlag) break;
                }
            }
            if (FoundFlag == true && ILibrary.SubsystemTime.PeriodicGameTimeEvent(1.0, 0.0)) FoundFlag = false;
        }
        public override bool Simulate()
        {
            IncreasePower();
            DigMineral();
            return base.Simulate();
        }

    }
}
